<template><div><h2 id="多语言基本使用" tabindex="-1"><a class="header-anchor" href="#多语言基本使用"><span>多语言基本使用</span></a></h2>
<p>在<code v-pre>Dcat Admin</code>中使用多语言翻译非常方便，数据表格、数据表单、数据详情和模型树的字段都支持自动读取语言包翻译，具体可参考<a href="https://learnku.com/docs/dcat-admin/1.x/field-translation/8104" target="_blank" rel="noopener noreferrer">表格字段翻译</a>、<a href="https://learnku.com/docs/dcat-admin/1.x/field-translation/8115" target="_blank" rel="noopener noreferrer">表单字段翻译</a>、<a href="https://learnku.com/docs/dcat-admin/1.x/field-translation/8121" target="_blank" rel="noopener noreferrer">数据详情字段翻译</a>。</p>
<h2 id="语言包文件" tabindex="-1"><a class="header-anchor" href="#语言包文件"><span>语言包文件</span></a></h2>
<p>语言包文件类型大概如下</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">resources/lang</span>
<span class="line">    ├── <span class="token punctuation">..</span>.</span>
<span class="line">    └── en</span>
<span class="line">        ├── admin.php   <span class="token comment"># 系统内容语言包</span></span>
<span class="line">        ├── menu.php    <span class="token comment"># 菜单标题语言包</span></span>
<span class="line">        ├── global.php  <span class="token comment"># 控制器公共语言包</span></span>
<span class="line">        ├── <span class="token punctuation">{</span>xxx<span class="token punctuation">}</span>.php   <span class="token comment"># 控制器语言包，一个控制器对应一个语言包</span></span>
<span class="line">        └── <span class="token punctuation">..</span>.         </span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>控制器语言包名称需要与控制器名相对应，例如控制器名<code v-pre>UserProfileController</code>，则对应的语言包为<code v-pre>resources/lang/{当前语言}/user-profile.php</code>（需要转化为小写中划线风格）。</p>
<h2 id="控制器语言包内容格式" tabindex="-1"><a class="header-anchor" href="#控制器语言包内容格式"><span>控制器语言包内容格式</span></a></h2>
<p>控制器语言包（包括<code v-pre>global.php</code>）中的内容分为三个类别：</p>
<ul>
<li><code v-pre>fields</code> 数据字段的翻译，这个类别下放置数据字段的翻译</li>
<li><code v-pre>labels</code> 自定义内容翻译，这个类别下是对数据字段外的内容翻译，可以是任何自定义内容</li>
<li><code v-pre>options</code> 枚举选项翻译</li>
</ul>
<p>下面是例子：</p>
<p>假设控制器语言包<code v-pre>user-profile.php</code>内容如下：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span> </span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'labels'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'UserProfile'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'用户中心'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'list'</span>        <span class="token operator">=></span> <span class="token string single-quoted-string">'列表'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line">        <span class="token string single-quoted-string">'pagination'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'range'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'从 :first 到 :last ，总共 :total 条'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'fields'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span>      <span class="token operator">=></span> <span class="token string single-quoted-string">'名称'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'published'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'发布'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'author'</span>    <span class="token operator">=></span> <span class="token string single-quoted-string">'作者'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'status'</span>    <span class="token operator">=></span> <span class="token string single-quoted-string">'状态'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'options'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token number">0</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'未激活'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token number">1</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'已激活'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>则语言包可以这么使用</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserProfileController</span> extend AdminController</span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 页面标题翻译，默认会自动读取，不需要指定</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// labels翻译示例，最终翻译成 “用户中心”</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">admin_trans_label</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'UserProfile'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    </span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// fields和options翻译示例</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">grid</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$grid</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Grid</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">UserProfile</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 显示调用语言包翻译，这里会把 “name” 字段翻译成 “名称”</span></span>
<span class="line">        <span class="token variable">$grid</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token function">admin_trans_field</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 隐式使用语言包翻译，“author” 字段会自动翻译成 “作者”</span></span>
<span class="line">        <span class="token variable">$grid</span><span class="token operator">-></span><span class="token property">author</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 调用options翻译</span></span>
<span class="line">        <span class="token variable">$grid</span><span class="token operator">-></span><span class="token function">status</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">using</span><span class="token punctuation">(</span><span class="token function">admin_trans</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user-profile.options.status'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$grid</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="语言包名称" tabindex="-1"><a class="header-anchor" href="#语言包名称"><span>语言包名称</span></a></h3>
<p>语言包名称需要与控制器名相对应，假如控制器名<code v-pre>UserProfileController</code>，则对应的语言包为<code v-pre>resources/lang/{当前语言}/user-profile.php</code>（需要转化为小写中划线风格）。</p>
<p>如果想要更改语言包的名称，可以通过下面两种方式进行更改</p>
<p>方式1</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Dcat<span class="token punctuation">\</span>Admin<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>AdminController</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">UserController</span> <span class="token keyword">extends</span> <span class="token class-name">AdminController</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 指定翻译文件名称</span>
<span class="line">     * </span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span> </span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$translation</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'user1'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token operator">...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>方式2</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Dcat<span class="token punctuation">\</span>Admin<span class="token punctuation">\</span>Admin</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Admin</span><span class="token operator">::</span><span class="token function">translation</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user1'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="使用" tabindex="-1"><a class="header-anchor" href="#使用"><span>使用</span></a></h2>
<h3 id="admin-trans-field" tabindex="-1"><a class="header-anchor" href="#admin-trans-field"><span>admin_trans_field</span></a></h3>
<p>这个函数用于翻译<code v-pre>fields</code>类别下内容，会自动找对应控制器下的翻译文件，如果翻译不存在会去找<code v-pre>global.php</code>中的翻译。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">admin_trans_field</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="admin-trans-label" tabindex="-1"><a class="header-anchor" href="#admin-trans-label"><span>admin_trans_label</span></a></h3>
<p>这个函数用于翻译<code v-pre>labels</code>类别下内容，会自动找对应控制器下的翻译文件，如果翻译不存在会去找<code v-pre>global.php</code>中的翻译。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">admin_trans_label</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Posts'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">admin_trans_label</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'pagination.range'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'first'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'last'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'total'</span> <span class="token operator">=></span> <span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="admin-trans-option" tabindex="-1"><a class="header-anchor" href="#admin-trans-option"><span>admin_trans_option</span></a></h3>
<p>这个函数用于翻译<code v-pre>options</code>类别下内容，会自动找对应控制器下的翻译文件，如果翻译不存在会去找<code v-pre>global.php</code>中的翻译。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">admin_trans_option</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'status'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="admin-trans" tabindex="-1"><a class="header-anchor" href="#admin-trans"><span>admin_trans</span></a></h3>
<p>此方法与<code v-pre>Laravel</code>框架自带的<code v-pre>trans</code>方法用法没有区别，唯一的区别是：当翻译的内容找不到时，会去<code v-pre>global.php</code>中再找一次。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 先去 user.php 中找 first_name 的翻译，如果找不到会去 global.php 中找</span></span>
<span class="line"><span class="token function">admin_trans</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user.first_name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="公共翻译文件" tabindex="-1"><a class="header-anchor" href="#公共翻译文件"><span>公共翻译文件</span></a></h2>
<p>所有常用的翻译都可以放在<code v-pre>resources/lang/{当前语言}/global.php</code>中，当控制器翻译文件不存在时会读取公共翻译文件翻译。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'fields'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'id'</span>                    <span class="token operator">=></span> <span class="token string single-quoted-string">'ID'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span>                  <span class="token operator">=></span> <span class="token string single-quoted-string">'名称'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'username'</span>              <span class="token operator">=></span> <span class="token string single-quoted-string">'用户名'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span>                 <span class="token operator">=></span> <span class="token string single-quoted-string">'邮箱'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'password'</span>              <span class="token operator">=></span> <span class="token string single-quoted-string">'密码'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'labels'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'list'</span>   <span class="token operator">=></span> <span class="token string single-quoted-string">'列表'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'edit'</span>   <span class="token operator">=></span> <span class="token string single-quoted-string">'编辑'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'detail'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'详细'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'create'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'创建'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'root'</span>   <span class="token operator">=></span> <span class="token string single-quoted-string">'顶级'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'scaffold'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'代码生成器'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'options'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="默认面包屑翻译" tabindex="-1"><a class="header-anchor" href="#默认面包屑翻译"><span>默认面包屑翻译</span></a></h2>
<p>例如你的访问路径是<code v-pre>/admin/my-users</code>，控制器是<code v-pre>MyUserController</code>，那么则可以在控制器对应的翻译文件中加上</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line"> <span class="token string single-quoted-string">'labels'</span> <span class="token operator">=></span> <span class="token punctuation">[</span> </span>
<span class="line">     <span class="token comment">// 跟路由路径相同</span></span>
<span class="line">     <span class="token string single-quoted-string">'my-users'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'用户'</span><span class="token punctuation">,</span></span>
<span class="line"> <span class="token punctuation">]</span><span class="token punctuation">,</span> </span>
<span class="line"> <span class="token operator">...</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></div></template>


